'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 战斗排行版
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var roleModel = require('../models/roleModel');
var bossModel = require('../models/bossModel');
var battleModel = require('../models/battleModel');
var soldierModel = require('../models/soldierModel.js');
var arenaModel = require('../models/arenaModel.js');
var PlayerManager = require('../libs/playerManager');
var unionGroupModel = require('../models/unionGroupModel');
var roleService = require('./roleService.js');
var soldierService = require('./soldierService.js');

/**
 * 查询Boss伤害排行版
 * @param {[type]} battle_list [description]
 */
exports.getBossDamageRank = function (cb) {
    _getBossDamageRankInfo(function (err, boss_list) {
        // 前50名玩家排行数据
        cb(null, boss_list);
    });
};

/**
 * 获取当前玩家伤害排名
 * @param {[type]} current_role_rank [description]
 */
exports.getCurrentRoleBossDamageRank = function (role_id, cb) {
    _getRoleBossDamageRank(role_id, function (err, current_role_info) {
        // 当前玩家排名
        cb(null, current_role_info);
    });
};

/**
 * 当前玩家排名
 */
var _getRoleBossDamageRank = function (role_id, cb) {
    var current_role_rank;
    var damage;

    var boss_model;
    var role_model;
    var battle_model;
    var soldier_model;
    var current_role_info = {};
    async.waterfall(
        [
            function (cb) {
                roleModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                role_model = model;
                battleModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                battle_model = model;
                bossModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                boss_model = model;
                damage = model.getMaxDamage();
                soldierModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                soldier_model = model;
                bossModel.getRankByRoleId(role_id, damage, cb);
            },
            function (rank, cb) {
                current_role_rank = rank;
                _getRoleInfo(role_model, cb);
            },
            function (role_info, cb) {
                current_role_info = role_info;
                current_role_info.max_damage = damage;
                current_role_info.fight_power = boss_model.fight_power;
                current_role_info.rank = current_role_rank;
                cb();
            }
        ],

        function (err) {
            if (!!err) {
                console.error(err);
            }
            cb(null, current_role_info);
        }
    );
};

/**
 * 玩家信息
 */
var _getRoleInfo = function (role_item, cb) {
    roleService.getRoleInfo(role_item.role_id, function (err, role_info) {
        if (!!role_info) {
            cb(null, role_info);
        } else {
            console.error('not found', role_item.role_id);
            cb(null, role_item);
        }
    });
};

/**
 * 排行版相关信息
 */
var _getBossDamageRankInfo = function (callback) {
    var rank_list = [];
    var top_fifty_role;
    var role_id_list = [];
    var soldier_map = {};
    var arena_map = {};
    var role_map = {};
    var where;
    async.waterfall(
        [
            function (cb) {
                bossModel.getTopRank(50, cb);
            },
            function (list, cb) {
                top_fifty_role = list;
                _.forEach(top_fifty_role, function (item) {
                    role_id_list.push(item.role_id);
                });

                where = {
                    role_id: {
                        $in: role_id_list
                    }
                };
                soldierModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (soldier) {
                    soldier_map[soldier.role_id] = soldier;
                });

                arenaModel.findWhere(where, cb);
            },
            function (list, cb) {
                _.forEach(list, function (arena) {
                    arena_map[arena.role_id] = arena;
                });

                roleService.getRoleList(role_id_list, cb);
            },
            function (list, cb) {
                _.forEach(list, function (role) {
                    role_map[role.role_id] = role;
                });

                var rank_num = 0;
                _.forEach(top_fifty_role, function (rank_info) {
                    rank_num++;
                    var role_id = rank_info.role_id;
                    var role_info = role_map[role_id];
                    var soldier_model = soldier_map[role_id];
                    var arena_model = arena_map[role_id];

                    role_info.max_damage = rank_info.max_damage;
                    role_info.fight_power = rank_info.fight_power;
                    role_info.rank = rank_num;
                    if (!!arena_model) {
                        role_info.arena_rank = arena_model.rank;
                    } else {
                        role_info.arena_rank = 0;
                    }
                    role_info.soldier_list = soldierService.getSoldierInfoList(rank_info.select_soldier_list, soldier_model);
                    rank_list.push(role_info);
                });
                cb();
            },
        ],
        function (err) {
            if (!!err) {
                return callback(null, []);
            }

            callback(null, rank_list);
        }
    );
};
